package com.choichumgming.rentcat.web.controller;


import com.choichumgming.rentcat.common.api.CommonResult;
import com.choichumgming.rentcat.model.PmsProduct;
import com.choichumgming.rentcat.service.PmsEsProductService;
import com.choichumgming.rentcat.service.PmsProductService;
import com.choichumgming.rentcat.service.UmsMemberFeignApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName PmsProductController
 * @Description : 商品管理Controller
 * @Author : choichumgming
 * @Date : 27/2/2020 22:16
 */
@Controller
@Api(tags = "PmsProductController", description = "商品管理")
@RequestMapping("/")
public class PmsProductController {
    @Autowired
    private PmsProductService productService;

    @Autowired
    private UmsMemberFeignApi umsMemberFeignApi;

    @Autowired
    private PmsEsProductService esProductService;

    @ApiOperation("创建商品")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
//    @PreAuthorize("hasAuthority('pms:product:create')")
    public CommonResult create(@RequestBody PmsProduct product) {
        int productId = productService.create(product);
//        esProductService.create((long)productId);
        if (productId > 0) {
            return CommonResult.success(productId);
        } else {
            return CommonResult.failed();
        }
    }


    @ApiOperation("更新商品")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
//    @PreAuthorize("hasAuthority('pms:product:update')")
    public CommonResult update(@PathVariable Long id, @RequestBody PmsProduct product) {
        int count = productService.update(id, product);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据商品id查询商品")
    @RequestMapping(value = "/product/{productId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmsProduct> getProductPathVariable(@PathVariable(value = "productId") Long productId) {
        PmsProduct product = productService.getProduct(productId);
        return CommonResult.success(product);
    }




    @ApiOperation("根据商品名称或货号模糊查询")
    @RequestMapping(value = "/simpleList", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<List<PmsProduct>> getList(String keyword) {
        List<PmsProduct> productList = productService.list(keyword);
        return CommonResult.success(productList);
    }


    @ApiOperation("批量上架")
    @RequestMapping(value = "/update/publish", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateUnpublishStatus(@RequestBody Long[] ids) {
        List<Long> resultList= new ArrayList<>(Arrays.asList(ids));

        int count = productService.updateProductsPublishStatus(resultList,  1);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("批量下架")
    @RequestMapping(value = "/update/unPublish", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updatePublishStatus(@RequestBody Long[] ids) {
        List<Long> resultList= new ArrayList<>(Arrays.asList(ids));

        int count = productService.updateProductsPublishStatus(resultList,  0);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }


    @ApiOperation("批量删除")
    @RequestMapping(value = "/delete/list", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult deleteList(@RequestBody Long[] ids) {
        List<Long> resultList= new ArrayList<>(Arrays.asList(ids));
        int count = productService.delete(resultList);
        esProductService.delete(resultList);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }


    @ApiOperation("获得指定用户的商品列表")
    @RequestMapping(value = "/list/{userId}", method = RequestMethod.GET)
    @ResponseBody
    CommonResult<List<PmsProduct>> getUserProductList(@PathVariable("userId") Long userId) {
        List<PmsProduct> productList = productService.getUserProductList(userId);
        return  CommonResult.success(productList);
    }

    @ApiOperation("随机获得带有Video的商品")
    @RequestMapping(value = "/video", method = RequestMethod.GET)
    @ResponseBody
    CommonResult getProductsWithVideo() {
        List<PmsProduct> productList = productService.getProductsWithVideo();
        return CommonResult.success(productList);
    }

    @ApiOperation("随机获得首页推荐商品")
    @RequestMapping(value = "/getHomeProductList", method = RequestMethod.GET)
    @ResponseBody
    CommonResult getHomeProductList() {
        List<PmsProduct> productList = productService.getHomeProductList();
        return CommonResult.success(productList);
    }

    @ApiOperation("获得首页banner")
    @RequestMapping(value = "/getBannnerList", method = RequestMethod.GET)
    @ResponseBody
    CommonResult getBannnerList() {
        List<PmsProduct> productList = productService.getBannnerList();
        return CommonResult.success(productList);
    }
}
